package com.ag.wzp.action;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.ag.common.AnnexPath;
import com.ag.common.BaseAction;
import com.ag.common.IPermissionService;
import com.ag.lzm.dao.TbUriDAO;
import com.ag.lzm.utils.PlanAnnex;
import com.ag.lzm.utils.PlanUtils;
import com.opensymphony.xwork2.ActionContext;
import common.Logger;
@Controller("mackupAnnexUploadAction")
public class MackupAnnexUploadAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(MackupAnnexUploadAction.class);
 
	@Resource(name = "LZMTbUriDAO")
	private TbUriDAO uriDAO;
	
	private List<File> Filedata;
	private List<String> FiledataFileName;
	private List<String> FiledataContentType;
	private String name;

	public String upload() {
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		logger.info(request.getParameter("EID"));
//		ServletContext sc = (ServletContext) ac
//				.get(ServletActionContext.SERVLET_CONTEXT);
//		String savePath = sc.getRealPath("/");
//		savePath = savePath + "wzpuploads/";
		/**
		 * 自定义硬盘映射路径
		 */
		// String fileUL ="F:MYTEST/";
		String fileUL = uriDAO.findById(AnnexPath.CASE_ANNEX_PATH).getUPath()+request.getParameter("EID")+"/";
		File f2 = new File(fileUL);
		if (!f2.exists()) {
			f2.mkdirs();
		}
		/**
		 * 获取上传文件数量
		 */
		int size = Filedata.size();
		if (size == 0)
			return ERROR;
		String extName = null;
		String name = null;
		for (int i = 0; i < size; i++) {
			// System.out.println("FiledataFileName:" +
			// FiledataFileName.get(i));
			extName = FiledataFileName.get(i).substring(
					FiledataFileName.get(i).lastIndexOf("."));
			name = UUID.randomUUID().toString();
			//addAnnex2Session(name + extName, FiledataFileName.get(i));
			File file = new File(fileUL + name + extName);
			try {
				FileUtils.copyFile(Filedata.get(i), file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.getWriter().print(name + extName);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<File> getFiledata() {
		return Filedata;
	}

	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}

	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public List getFiledataContentType() {
		return FiledataContentType;
	}

	public void setFiledataContentType(List<String> filedataContentType) {
		FiledataContentType = filedataContentType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAllFile() {
		return null;
	}

//	/**
//	 * 把附件文件信息放到Session里面
//	 * 
//	 * @param paId
//	 *            文件ID，即服务器硬盘存储的值
//	 * @param paName
//	 *            客户原来的文件名
//	 */
//	private void addAnnex2Session(String paId, String paName) {
//		List<PlanAnnex> paList = (List<PlanAnnex>) session
//				.get(PlanUtils.PLAN_ANNEX);
//		PlanAnnex pa = new PlanAnnex();
//		pa.setPaId(paId);
//		pa.setPaName(paName);
//		if (paList == null) {
//			paList = new ArrayList<PlanAnnex>();
//			paList.add(pa);
//		} else {
//			paList.add(pa);
//		}
//		session.remove(PlanUtils.PLAN_ANNEX);
//		session.put(PlanUtils.PLAN_ANNEX, paList);
//	}

}
